package com.holyn.guofang.view.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.R;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.view.distinguish.CommonDistinguishActivity;
import com.holyn.guofang.view.distinguish.PictureConfirmActivity;
import com.holyn.guofang.view.search.SeniorSearchActivity;
import com.holyn.guofang.zxing.activity.CaptureActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.bottomBar)
    BottomBar bottomBar;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private int fragmentIndex = 0;

    HomeDistinguishFragment homeDistinguishFragment = new HomeDistinguishFragment();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_home;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.d("=======> onRestart 。。。。" );
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
//        LogUtils.d("=======> savedInstanceState = "+savedInstanceState );
//        if (savedInstanceState == null) {
//            // 添加显示第一个fragment
//            homeDistinguishFragment = new HomeDistinguishFragment();
//        } else {
//            homeDistinguishFragment = (HomeDistinguishFragment) getSupportFragmentManager()
//                    .findFragmentByTag("distinguish");
//        }
//        replaceFragment(new HomeDistinguishFragment(), "distinguish");
    }

    private void initView() {
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                switch (tabId) {
                    case R.id.tab_brand:
                        hideToolbarMenuRight();
                        showToolbar(getResources().getString(R.string.title_brand), false);
                        replaceFragment(homeDistinguishFragment, "distinguish");
                        fragmentIndex = 0;
                        break;
                    case R.id.tab_search:
//                        showToolbarMenuRight(getResources().getString(R.string.title_search), false, getResources().getString(R.string.senior_search)
//                                , new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        startActivity(new Intent(HomeActivity.this, SeniorSearchActivity.class));
//                                    }
//                                });
                        hideToolbarMenuRight();
                        showToolbar(getResources().getString(R.string.title_search), false);
                        replaceFragment(new HomeSearchFragment(), "search");
                        fragmentIndex = 1;
                        break;
                    case R.id.tab_web:
                        hideToolbarMenuRight();
                        showToolbar(getResources().getString(R.string.title_web), false);
                        replaceFragment(new HomeWebFragment(), "web");
                        fragmentIndex = 2;
                        break;
                    case R.id.tab_my:
                        showToolbarMenuRight(getResources().getString(R.string.title_my), false, R.drawable.ic_qrcode       //getResources().getString(R.string.erweima_my)
                                , new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        startActivity(new Intent(HomeActivity.this, CaptureActivity.class));
                                    }
                                });
                        replaceFragment(new HomeMyFragment(), "my");
                        fragmentIndex = 3;
                        break;
                }
            }
        });
    }

    private void replaceFragment(Fragment fragment, String tag){
        FragmentTransaction  fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fl_content, fragment, tag);
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {
        if (fragmentIndex != 0){
            bottomBar.selectTabAtPosition(0);
        } else {
            Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
            mHomeIntent.addCategory(Intent.CATEGORY_HOME);
            mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            startActivity(mHomeIntent);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //在HomeDistinguishFragment的onActivityResult有时不执行，bug
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
//                case PictureConfig.CHOOSE_REQUEST:
//                    // 图片选择结果回调
//                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
//                    // 例如 LocalMedia 里面返回三种path
//                    // 1.media.getPath(); 为原图path
//                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
//                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
//                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
//                    LogUtils.d(selectList.get(0));
//                    String imagePath = selectList.get(0).getCompressPath();
//                    CommonDistinguishActivity.start(HomeActivity.this, imagePath);
//                    break;
                case PictureConfirmActivity.REQUEST_CODE_OPEN_ALBUM:
                case PictureConfirmActivity.REQUEST_CODE_TAKE_PHOTO:
                    String imagePath = data.getStringExtra(PictureConfirmActivity.INTENT_KEY_IMAGE_PATH);
                    CommonDistinguishActivity.start(HomeActivity.this, imagePath);
                    break;
            }
        }
    }
}
